package com.kgis.map.handler.feature.draw {
    import com.kgis.map.Map;
    import com.kgis.map.common.enum.GeoTypeEnum;
    import com.kgis.map.common.enum.HandlerEnum;
    import com.kgis.map.control.Blackboard;
    import com.kgis.map.feature.Feature;
    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import com.kgis.map.handler.feature.draw.AbstractEditHandler;
    import com.kgis.map.handler.feature.draw.EditLineHandler;
    import com.kgis.map.handler.feature.draw.EditPointHandler;
    import com.kgis.map.handler.feature.draw.EditPolygonHandler;

    /**
     * 编辑工具
     * */
    public class EditHandler {
        private var _map:Map;
        private var handler:AbstractEditHandler;
        //可捕捉的图层ids
        private var _snapClickCallback:Function = null;


        /**
         * move any feature
         * */
        public static const MOVE:Number = 1;
        /**
         * drag the vertices of the feature.<br>
         * only support the polyline geometry and pologon geometry.
         * */
        public static const DRAG_VERTICES:Number = 2;
        /**
         * add the vertice for the editingfeature <br>
         * only support the polyline geometry and pologon geometry.
         * */
        public static const ADD_VERTICES:Number = 4;
        /**
         * raotate the selected feature<br>
         * only support the point geometry.
         * */
        public static const ROTATE:Number = 8;
        /**
         *delete vertice
         * */
        public static const DELETE_VERTICES:Number = 16;
        //Associated moving
        public static const MOVE_ASSOCIATED:int = 32;

        public var feature:Feature;

        public function EditHandler(map:Map, editFeature:Feature) {
            feature = editFeature;
            _map = map;
            if (editFeature == null) {
                Alert.show("请先选择要编辑的设施,", "提示");
                return;
            }
            switch (editFeature.geometry.getGeometryType()) {
                case GeoTypeEnum.POINTGEOMETRY:
                    handler = map.getHandler(HandlerEnum.EDIT_POINT_HANDLER) as EditPointHandler;
                    break;
                case GeoTypeEnum.POLYLINEGEOMETRY:
                    handler = map.getHandler(HandlerEnum.EDIT_LINE_HANDLER) as EditLineHandler;
                    break;
                case GeoTypeEnum.POLYGONGEOMETRY:
                    handler = map.getHandler(HandlerEnum.EDIT_POLYGON_HANDLER) as EditPolygonHandler;
                    break;
            }
            handler.editFeature = editFeature;
        }

        public function set active(value:Boolean):void {

            if (value) {
                handler.blackBoard = (this._map.getControlByName("blackBoard") as Blackboard);
                    //handler.blackBoard.visible=true;

            }
            else {
                //删除黑板
                handler.blackBoard.visible = false;
                handler.blackBoard.initialize();
            }
            handler.active = value;
        }

        public function set snapLayerIds(value:ArrayCollection):void {
            handler.snapLayerIds = value;
        }

        public function set snapClickCallback(value:Function):void {
            handler.snapClickCallback = value;
        }

        /**
         * Activates the tool for editing features. Once called the tool is ready to respond to user interaction based on specified edit type.
         * @param editType
         * 			——it can only support the following type.<br>
         * 			EditHandler.MOVE:move the feature.<br>
         * 			EditHandler.DRAG_VERTICES: drag the vertices of the feature.<br>
         * 			EditHandler.EDIT_VERTICES:add vertice or delete vertice.<br>
         * 			EditHandler.DELETE:delete the feature.<br>
         * */
        public function activateEditMode(editType:Number):void {
            handler.activateEditMode(editType);
        }

        /**
         *Deactivates the edit tool.
         * */
        public function deactivateEditMode(editType:Number):void {
            handler.deactivateEditMode(editType);
        }

        public function get snapTolerance():int {
            return handler.snapTolerance;
        }

        public function set snapTolerance(value:int):void {
            handler.snapTolerance = value;
        }

    }
}